//
//  YXMachineMaterialViewController.m
//  CreditCard
//
//  Created by 杨旭 on 2017/12/24.
//  Copyright © 2017年 杨旭. All rights reserved.
//

#import "YXMachineMaterialViewController.h"
#import "YXMachineMaterialTableView.h"
#import "YXMachineDetailsTableView.h"
#import "YXMachinePopView.h"
#import "YXMineAddressViewController.h"
#import "YXMachinesViewModel.h"
#import "YXMachinesModel.h"
#import "YXMineWorkOrderViewController.h"
@interface YXMachineMaterialViewController ()<UIWebViewDelegate>
@property (nonatomic ,strong) YXMachineMaterialTableView *tableView;
@property (nonatomic ,strong) YXMachineDetailsTableView *detailsTableView;
@property (nonatomic ,strong) YXMachinePopView *popView;
@end

@implementation YXMachineMaterialViewController

- (YXMachineMaterialTableView *)tableView {
    if (!_tableView) {
        _tableView = [[YXMachineMaterialTableView alloc] initWithFrame:(CGRectMake(0, 0, kScreenWidth, kScreenHeight- 64)) style:(UITableViewStylePlain)];
        YXWeakSelf
        [_tableView setSelectRowAtIndexPath:^(NSInteger row,NSString *title,YXMachinesModel *model) {
            YXMachineMaterialViewController *typeVC = [[YXMachineMaterialViewController alloc] init];
            typeVC.typeView = Machine_DetailView;
            typeVC.model = model;
            [weakSelf.navigationController pushViewController:typeVC animated:YES];
        }];
    }
    return _tableView;
}

- (YXMachineDetailsTableView *)detailsTableView {
    if (!_detailsTableView) {
        _detailsTableView = [[YXMachineDetailsTableView alloc] initWithFrame:(CGRectMake(0, 0, kScreenWidth, kScreenHeight - 64 - 50)) style:(UITableViewStylePlain)];
        _detailsTableView.detailsModel = self.model;
    }
    return _detailsTableView;
}

#pragma mark - toolBarUI
- (UIView *)createToolBarUI {
    
    UIView *view = [[UIView alloc] initWithFrame:(CGRectMake(0, kScreenHeight - 50-64, kScreenWidth, 50))];
    view.backgroundColor = [UIColor whiteColor];
    
    UIView *lineView = [[UIView alloc] initWithFrame:(CGRectMake(0, 0, kScreenWidth, 1))];
    lineView.backgroundColor = color_lineColor;
    [view addSubview:lineView];
    
    UIButton *applyBtn = [UIButton buttonWithType:(UIButtonTypeCustom)];
    applyBtn.frame = CGRectMake(kScreenWidth - 130, 10, 120, 30);
    applyBtn.backgroundColor = color_globalColor;
    applyBtn.titleLabel.font = [UIFont systemFontOfSize:16];
    [applyBtn setTitle:@"立即申请" forState:(UIControlStateNormal)];
    [applyBtn setTitleColor:[UIColor whiteColor] forState:(UIControlStateNormal)];
    applyBtn.layer.masksToBounds = YES;
    applyBtn.layer.cornerRadius = 15.0f;
    [applyBtn addTarget:self action:@selector(applyBtnAction:) forControlEvents:(UIControlEventTouchUpInside)];
    [view addSubview:applyBtn];
    return view;
}

- (void)applyBtnAction:(UIButton *)sender {
    
    [UIView animateWithDuration:0.0 animations:^{
        [self createPopView];
    }];
    
}

- (void)createPopView {
    self.popView = [[YXMachinePopView alloc] initWithFrame:(CGRectMake(0, 0, kScreenWidth, kScreenHeight-64)) withDataArray:nil];
    self.popView.model = self.model;
    [self.view addSubview:self.popView];
    
    YXWeakSelf
    // 选择收货地址
    [self.popView setClickChangeAddressBtnBlock:^{
        YXMineAddressViewController *addressVC = [[YXMineAddressViewController alloc] init];
        [weakSelf.navigationController pushViewController:addressVC animated:YES];
        
        [addressVC setSelectAddressBlock:^(NSString *address) {
            
            [weakSelf.popView setAddress:address];
        }];
    }];
    
    // 点击添加订单
    [self.popView setClickConfirmBtnBlock:^(NSString *num, NSString *address, NSString *totalPrice,NSString *goodsId) {
        NSLog(@"%@-%@-%@-%@",num,address,totalPrice,goodsId);
        NSDictionary *dict = @{@"goodsId":goodsId,@"num":num};
        NSArray *goodArr = @[dict];
        NSString *goods = [BX_NetWorkManager arrayToString:goodArr];
        NSLog(@"%@",goods);
        [YXMachinesViewModel getQueryAddOrderById:[YXUserInfoManager getUserInfo].sessionId Goods:goods TotalPrice:totalPrice OrdersTypes:@"0" ModelList:^(NSArray *array) {
            
            UIAlertController *alertController = [UIAlertController alertControllerWithTitle:@"温馨提示" message:@"订单添加成功，是否查看详情" preferredStyle:(UIAlertControllerStyleAlert)];
            
            UIAlertAction *alert = [UIAlertAction actionWithTitle:@"取消" style:(UIAlertActionStyleDefault) handler:^(UIAlertAction * _Nonnull action) {
                
            }];
            UIAlertAction *alert1 = [UIAlertAction actionWithTitle:@"确定" style:(UIAlertActionStyleDefault) handler:^(UIAlertAction * _Nonnull action) {
                YXMineWorkOrderViewController *workVC = [[YXMineWorkOrderViewController alloc] init];
                [weakSelf.navigationController pushViewController:workVC animated:YES];
            }];
            
            [alertController addAction:alert];
            [alertController addAction:alert1];
            
            [weakSelf presentViewController:alertController animated:YES completion:nil];
            
      
        } Failure:^(NSError *error) {
            [weakSelf showTostMessage:@"正在维护中！"];
        }];
        
        
        
    }];
    
}


- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor whiteColor];
    [self createLeftBarButtonItemWithImage:@"导航栏_002"];
  
    
    switch (self.typeView) {
        case Machine_MaterialView:
        {
            self.title = @"机具物料";
            self.tableView.title = self.title;
            [self.view addSubview:self.tableView];
            [self requestData];
        }
            break;
        case Machine_DetailView:
        {
            self.title = @"机具详情";
            [self.view addSubview:self.detailsTableView];
            [self.view addSubview:[self createToolBarUI]];
            
        }
            break;
        case Machine_RepairOrder:
        {
            self.title = @"我的工单";
            [self.view addSubview:self.tableView];
        }
            break;
        default:
            break;
    }
    
}

- (void)requestData {
    YXUserInfoModel *model = [YXUserInfoManager getUserInfo];
    [YXMachinesViewModel getQueryGoodslistById:model.sessionId ModelList:^(NSArray *array) {
        self.tableView.dataArr = array;
        [self.tableView reloadData];
    } Failure:^(NSError *error) {
        [self showAlertWithTitle:@"提示" Messge:@"维护中，请稍等！"];

    }];
    
}


@end
